﻿#include "stdafx.h"
#include "PressKeyUtil.h"
#include "DefInfo/GameInfo.h"
#include "CaptureTool.h"
#include "Dialog\ClickerDlg.h"


PressKeyUtil::PressKeyUtil()
{
	Init();
}


PressKeyUtil::~PressKeyUtil()
{
}

PressKeyUtil press_key_util;

PressKeyUtil& PressKeyUtil::inst()
{
	return press_key_util;
}

void PressKeyUtil::Init()
{
	for (int key_index = 1; key_index <= 12; key_index++)
	{
		if (key_index >= 1 && key_index <= 9)
		{
			auto value = '0' + key_index;
			map_front_key_[key_index] = value;
			value = VK_NUMPAD0 + key_index;
			map_number_key_[key_index] = value;
		}
	}

	for (int action_index = 1; action_index <= 12; action_index++)
	{
		map_action_keys_[action_index] = { map_front_key_[action_index] };
	}
	for (int action_index = 49; action_index <= 60; action_index++)
	{
		int key_index = action_index % 48;
		map_action_keys_[action_index] = { VK_LSHIFT, map_front_key_[key_index] };
	}
	for (int action_index = 25; action_index <= 36; action_index++)
	{
		int key_index = action_index % 24;
		map_action_keys_[action_index] = { VK_LMENU, map_front_key_[key_index] };
	}
	for (int action_index = 61; action_index <= 72; action_index++)
	{
		int key_index = action_index % 60;
		map_action_keys_[action_index] = { VK_LMENU, map_number_key_[key_index] };
	}
}

void PressKeyUtil::SendKeyBySpellID(int spell_id)
{
	if (send_lock)
	{
		return;
	}
	auto& action = GameInfo::inst().GetActionBySpellId(spell_id);
	if (action.key_index != 0)
	{
	}
	auto it = map_action_keys_.find(action.key_index);
	if (it != map_action_keys_.end())
	{
		press_keys keys = it->second;
		SendKey(keys, action.key_index);
	}
	else
	{
		//
		OutputDebugStringA("无法找到对应的技能");
	}

	if (ready_Lock)
	{
		send_lock = true;
	}
}

void PressKeyUtil::SendKey(unsigned char key)
{
	if (send_lock)
	{
		return;
	}
	DWORD now = GetTickCount();
	DWORD use_time = 0;
	if (time_last_press == 0)
	{
		time_last_press = now;
	}
	else
	{
		use_time = now - time_last_press;
		time_last_press = now;
	}
	// VK_F5
	::PostMessage(CaptureTool::inst().GetWowHWnd(), WM_KEYDOWN, key, 1);
	Sleep(50);
	::PostMessage(CaptureTool::inst().GetWowHWnd(), WM_KEYUP, key, 1);
	// WriteActionList(key, use_time);
	if (ready_Lock)
	{
		send_lock = true;
	}
}

void PressKeyUtil::SendKey(std::vector<unsigned char>& keys, int key_index)
{
	if (send_lock)
	{
		return;
	}
	DWORD now = GetTickCount();
	DWORD use_time = 0;
	if (time_last_press == 0)
	{
		time_last_press = now;
	}
	else
	{
		use_time = now - time_last_press;
		time_last_press = now;
	}
	for (int i = 0; i < keys.size(); i++)
	{
		::PostMessage(CaptureTool::inst().GetWowHWnd(), WM_SYSKEYDOWN, keys[i], 0);
	}
	Sleep(50);
	for (int i = 0; i < keys.size(); i++)
	{
		::PostMessage(CaptureTool::inst().GetWowHWnd(), WM_SYSKEYUP, keys[keys.size() - i - 1], 1);
	}
	WriteActionList(key_index, use_time);
	if (ready_Lock)
	{
		send_lock = true;
	}
}

void PressKeyUtil::WriteActionList(int key_index, DWORD use_time)
{
	if (key_index == 0)
	{
		return;
	}
	//
	if (CClickerDlg::getInst() != NULL)
	{
		auto& dlg = *CClickerDlg::getInst();
		int count = dlg.list_action_history_.GetItemCount();
		CString text_info;
		CString str_index;
		str_index.Format(L"%d", count);
		text_info.Format(L"按键 %d ", key_index);
		DWORD now = GetTickCount();
		if (last_key_index != key_index || (key_index == last_key_index && (now - last_action_tick) > 500))
		{
			auto& action = GameInfo::inst().GetActionByKeyIndex(key_index);

			dlg.list_action_history_.InsertItem(count, str_index);
			auto& spell_config = GameInfo::inst().GetSpellConfigBySpellID(action.spell_id);
			text_info.Append(spell_config.name_cn);
			dlg.list_action_history_.SetItemText(count, 1, text_info);
			text_info.Format(L"%d", use_time);
			dlg.list_action_history_.SetItemText(count, 2, text_info);
			dlg.list_action_history_.EnsureVisible(count, FALSE);

			last_action_tick = now;
			last_key_index = key_index;
		}
	}
}
